<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Web Solutions
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: mslaria $ ($Date: 2006/09/11 22:32:16 $)
// $RCSfile: client_index.php,v $ : $Revision: 1.18 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================


if (!defined("INSWIFT")) {
	trigger_error("Unable to process $PHP_SELF", E_USER_ERROR);
}

$template->loadLanguageSection("index");
$template->cache(array('index', 'header', 'footer', 'navbar', 'dllatestfilesquickbox', 'dlpopularfilesquickbox', 'newsnavbar', 'tsquickbox', 'kbpopulararticlesqb', 'kblatestarticlesqb'));

$template->assign("navigation", '<a href="index.php" id="navlink">'.$_SWIFT["language"]["navhome"]."</a>");

if ($_GET["loginresult"] == -1)
{
	$template->assign("errormessage", $_SWIFT["language"]["loginerrorinvalidsession"]);
} else if ($_GET["loginresult"] == -2) {
	$template->assign("errormessage", $_SWIFT["language"]["loginerrorinvalidmodule"]);
} else if ($_GET["loginresult"] == -5 || $_GET["loginresult"] == "0") {
	$template->assign("errormessage", $_SWIFT["language"]["loginerror"]);
} else if ($_GET["loginresult"] == 1 && !empty($_SWIFT["user"]["userid"])) {
	$template->assign("infomessage", $_SWIFT["language"]["loginsuccess"]);
}

/**
* TROUBLESHOOTER
*/
if ($_SWIFT["settings"]["tr_enablepopularcat"] == 1 && $_SWIFT["settings"]["tr_enableclient"] == "1" && $module->isRegistered(MODULE_TROUBLESHOOTER)&& $_SWIFT["user"]["permissions"]["perm_canviewtroubleshooter"] != "0")
{
	$maxpopularcat = intval($_SWIFT["settings"]["tr_maxpopularcat"]);
	if (empty($maxpopularcat))
	{
		$_SWIFT["settings"]["tr_maxpopularcat"] = 5;
	}

	$troubleshootercatlist = $troubleshootercatidlist = array();
	$trcount = $index = 0;
	// Now get all the troubleshooters based on max views
	$dbCore->query("SELECT troubleshootercat.* FROM `". TABLE_PREFIX ."troubleshootercat` AS troubleshootercat LEFT JOIN `". TABLE_PREFIX ."tgroupassigns` AS tgroupassigns ON (troubleshootercat.troubleshootercatid = tgroupassigns.toassignid AND tgroupassigns.type = '". TGROUP_TROUBLESHOOTER ."') WHERE tgroupassigns.tgroupid = '". intval($_SWIFT["tgroup"]["tgroupid"]) ."' ORDER BY troubleshootercat.views DESC LIMIT ".intval($_SWIFT["settings"]["tr_maxpopularcat"]).";");
	while ($dbCore->nextRecord())
	{
		$trcount++;
		$troubleshootercatlist[$index] = $dbCore->Record;
		$troubleshootercatlist[$index]["number"] = $trcount;
		$troubleshootercatlist[$index]["rowclassno"] = ($index%2)+1;
		$index++;
	}

	if ($trcount)
	{
		$template->assign("istroubleshooterquickbox", "1");
		$template->assign("quickboxtr", $troubleshootercatlist);
	}
}

if ($_SWIFT["settings"]["nw_enablenewsnav"] == 1 && $_SWIFT["settings"]["nw_enableclient"] == "1" && $module->isRegistered(MODULE_NEWS) && $_SWIFT["user"]["permissions"]["perm_canviewnews"] != "0") {
	$maxnewsitems = intval($_SWIFT["settings"]["nw_maxnewslist"]);
	if (empty($maxnewsitems))
	{
		$maxnewsitems = $_SWIFT["settings"]["nw_maxnewslist"] = 6;
	}

	$newslist = array();
	$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."news` AS news WHERE news.tgroupid = '". intval($_SWIFT["tgroup"]["tgroupid"]) ."' AND news.newstype = 'public' AND (news.expiry >= '". DATENOW ."' OR news.expiry = '0') ORDER BY news.newsid DESC LIMIT ". intval($maxnewsitems) .";");
	$index = 1;
	while ($dbCore->nextRecord())
	{
		$newslist[$index] = $dbCore->Record;
		$newslist[$index]["displaydate"] = edate($_SWIFT["settings"]["nw_navdateformat"], $dbCore->Record["dateline"]);
		$index++;
	}

	if (count($newslist))
	{
		$template->assign("innewsnav", "1");
		$template->assign("latestnewscount", count($newslist));
		$template->assign("newsnavdata", $newslist);
	}
}

if ($_SWIFT["settings"]["dl_enpopularfiles"] == 1 && $_SWIFT["settings"]["dl_enableclient"] == 1 && $module->isRegistered(MODULE_DOWNLOADS) && $_SWIFT["user"]["permissions"]["perm_canviewdownloads"] != "0")
{
	require_once ("./includes/data_mimelist.php");
	$maxdlitems = intval($_SWIFT["settings"]["dl_maxpopularfiles"]);
	if (empty($maxdlitems))
	{
		$maxdlitems = $_SWIFT["settings"]["dl_maxpopularfiles"] = 6;
	}

	$itemlist = array();
	$dbCore->query("SELECT downloaditems.*, downloadcategories.*, tgroupassigns.toassignid, downloaditems.title AS filetitle, downloaditems.dateline AS itemdateline FROM `". TABLE_PREFIX ."downloaditems` AS downloaditems LEFT JOIN `". TABLE_PREFIX ."downloadcategories` AS downloadcategories ON (downloaditems.downloadcategoryid = downloadcategories.downloadcategoryid) LEFT JOIN `". TABLE_PREFIX ."tgroupassigns` AS tgroupassigns ON (downloadcategories.downloadcategoryid = tgroupassigns.toassignid AND tgroupassigns.type = '". TGROUP_DOWNLOADS ."') WHERE (tgroupassigns.tgroupid = '". intval($_SWIFT["tgroup"]["tgroupid"]) ."') OR (downloaditems.downloadcategoryid = 0) ORDER BY downloaditems.totaldownloads DESC LIMIT ". intval($maxdlitems) .";");
	$index = 1;
	$cntindex = 0;
	while ($dbCore->nextRecord())
	{
		if ($dbCore->Record["categorytype"] == SWIFTPRIVATE)
		{
			continue;
		}
		$itemlist[$index] = $dbCore->Record;
		$itemlist[$index]["displaydate"] = edate($_SWIFT["settings"]["dl_dispdateformat"], $dbCore->Record["itemdateline"]);
		$itemlist[$index]["rowclassno"] = ($cntindex%2)+1;
		$extension = getFileExtension($dbCore->Record["filename"]);
		$mimedata = $mimelist[$extension];
		if (!empty($mimedata[1]))
		{
			$itemlist[$index]["mimeicon"] = $mimedata[1];
		}
		$index++; $cntindex++;
	}

	if (count($itemlist))
	{
		$template->assign("ispopularfilesqb", "1");
		$template->assign("quickboxpopularfiles", $itemlist);
	}
}

if ($_SWIFT["settings"]["dl_enlatestfiles"] == 1 && $_SWIFT["settings"]["dl_enableclient"] == 1 && $module->isRegistered(MODULE_DOWNLOADS) && $_SWIFT["user"]["permissions"]["perm_canviewdownloads"] != "0")
{
	require_once ("./includes/data_mimelist.php");
	$maxdlitems = intval($_SWIFT["settings"]["dl_maxlatestfiles"]);
	if (empty($maxdlitems))
	{
		$maxdlitems = $_SWIFT["settings"]["dl_maxlatestfiles"] = 6;
	}

	$itemlist = array();
	$dbCore->query("SELECT downloaditems.*, downloadcategories.*, tgroupassigns.toassignid, downloaditems.title AS filetitle, downloaditems.dateline AS itemdateline FROM `". TABLE_PREFIX ."downloaditems` AS downloaditems LEFT JOIN `". TABLE_PREFIX ."downloadcategories` AS downloadcategories ON (downloaditems.downloadcategoryid = downloadcategories.downloadcategoryid) LEFT JOIN `". TABLE_PREFIX ."tgroupassigns` AS tgroupassigns ON (downloadcategories.downloadcategoryid = tgroupassigns.toassignid AND tgroupassigns.type = '". TGROUP_DOWNLOADS ."') WHERE (tgroupassigns.tgroupid = '". intval($_SWIFT["tgroup"]["tgroupid"]) ."') OR (downloaditems.downloadcategoryid = 0) ORDER BY downloaditems.downloaditemid DESC LIMIT ". intval($maxdlitems) .";");
	$index = 1;
	$cntindex = 0;
	while ($dbCore->nextRecord())
	{
		if ($dbCore->Record["categorytype"] == SWIFTPRIVATE)
		{
			continue;
		}
		$itemlist[$index] = $dbCore->Record;
		$itemlist[$index]["displaydate"] = edate($_SWIFT["settings"]["dl_dispdateformat"], $dbCore->Record["itemdateline"]);
		$itemlist[$index]["rowclassno"] = ($cntindex%2)+1;
		$extension = getFileExtension($dbCore->Record["filename"]);
		$mimedata = $mimelist[$extension];
		if (!empty($mimedata[1]))
		{
			$itemlist[$index]["mimeicon"] = $mimedata[1];
		}
		$index++; $cntindex++;
	}

	if (count($itemlist))
	{
		$template->assign("islatestfilesqb", "1");
		$template->assign("quickboxlatestfiles", $itemlist);
	}
}

if ($_SWIFT["settings"]["kb_enpopulararticles"] == 1 && $_SWIFT["settings"]["kb_enableclient"] == 1 && $module->isRegistered(MODULE_KNOWLEDGEBASE) && $_SWIFT["user"]["permissions"]["perm_canviewknowledgebase"] != "0")
{
	$maxitems = intval($_SWIFT["settings"]["kb_maxpopulararticles"]);
	if (empty($maxitems))
	{
		$maxitems = $_SWIFT["settings"]["kb_maxpopulararticles"] = 6;
	}

	$itemlist = $kbarticleidlist = array();

	$dbCore->query("SELECT kbarticles.*, kbarticlelinks.*, kbcategories.categorytype FROM `". TABLE_PREFIX ."kbarticles` AS kbarticles LEFT JOIN `". TABLE_PREFIX ."kbarticlelinks` AS kbarticlelinks ON (kbarticles.kbarticleid = kbarticlelinks.kbarticleid) LEFT JOIN `". TABLE_PREFIX ."kbcategories` AS kbcategories ON (kbarticlelinks.kbcategoryid = kbcategories.kbcategoryid) LEFT JOIN `". TABLE_PREFIX ."tgroupassigns` AS tgroupassigns ON (kbarticlelinks.kbcategoryid = tgroupassigns.toassignid AND tgroupassigns.type = '". TGROUP_KNOWLEDGEBASE ."') WHERE (tgroupassigns.tgroupid = '". intval($_SWIFT["tgroup"]["tgroupid"]) ."') OR (kbarticlelinks.kbcategoryid = 0) ORDER BY kbarticles.views DESC LIMIT ". intval($maxitems) .";");
	$index = 1;
	while ($dbCore->nextRecord())
	{
		if ($dbCore->Record["articlestatus"] != "published" || $dbCore->Record["categorytype"] != "public")
		{
			continue;
		}

		if (!in_array($dbCore->Record["kbarticleid"], $kbarticleidlist))
		{
			$kbarticleidlist[] = $dbCore->Record["kbarticleid"];
			$itemlist[$index] = $dbCore->Record;
			$itemlist[$index]["displaydate"] = edate($_SWIFT["settings"]["kb_dateformat"], $dbCore->Record["dateline"]);
			$itemlist[$index]["rowclassno"] = ($cntindex%2)+1;
			$index++; $cntindex++;
		}
	}

	if (count($itemlist))
	{
		$template->assign("ispopulararticlesqb", "1");
		$template->assign("qbpopulararticles", $itemlist);
	}
}

if ($_SWIFT["settings"]["kb_enlatestarticles"] == 1 && $_SWIFT["settings"]["kb_enableclient"] == 1 && $module->isRegistered(MODULE_KNOWLEDGEBASE) && $_SWIFT["user"]["permissions"]["perm_canviewknowledgebase"] != "0")
{
	$maxitems = intval($_SWIFT["settings"]["kb_maxlatestarticles"]);
	if (empty($maxitems))
	{
		$maxitems = $_SWIFT["settings"]["kb_maxlatestarticles"] = 6;
	}

	$itemlist = $kbarticleidlist = array();
	$dbCore->query("SELECT kbarticles.*, kbarticlelinks.*, kbcategories.categorytype FROM `". TABLE_PREFIX ."kbarticles` AS kbarticles LEFT JOIN `". TABLE_PREFIX ."kbarticlelinks` AS kbarticlelinks ON (kbarticles.kbarticleid = kbarticlelinks.kbarticleid) LEFT JOIN `". TABLE_PREFIX ."kbcategories` AS kbcategories ON (kbarticlelinks.kbcategoryid = kbcategories.kbcategoryid) LEFT JOIN `". TABLE_PREFIX ."tgroupassigns` AS tgroupassigns ON (kbarticlelinks.kbcategoryid = tgroupassigns.toassignid AND tgroupassigns.type = '". TGROUP_KNOWLEDGEBASE ."') WHERE (tgroupassigns.tgroupid = '". intval($_SWIFT["tgroup"]["tgroupid"]) ."') OR (kbarticlelinks.kbcategoryid = 0) ORDER BY kbarticles.kbarticleid DESC LIMIT ". intval($maxitems) .";");
	$index = 1;
	while ($dbCore->nextRecord())
	{
		if ($dbCore->Record["articlestatus"] != "published" || ($dbCore->Record["categorytype"] != "public" && $dbCore->Record["categorytype"] != ""))
		{
			continue;
		}
		if (!in_array($dbCore->Record["kbarticleid"], $kbarticleidlist))
		{
			$kbarticleidlist[] = $dbCore->Record["kbarticleid"];
			$itemlist[$index] = $dbCore->Record;
			$itemlist[$index]["displaydate"] = edate($_SWIFT["settings"]["kb_dateformat"], $dbCore->Record["dateline"]);
			$itemlist[$index]["rowclassno"] = ($cntindex%2)+1;
			$index++; $cntindex++;
		}
	}

	if (count($itemlist))
	{
		$template->assign("islatestarticlesqb", "1");
		$template->assign("qblatestarticles", $itemlist);
	}
}

echo $template->displayTemplate("index");
?>